package com.atguigu.flink.day09;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2024/8/19
 * 该案例演示了键控状态-列表状态
 * 需求：针对每种传感器输出最高的3个水位值
 */
public class Flink01_KeyedState_ListState {
    public static void main(String[] args) throws Exception {
        //TODO 1.环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(1);
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对流中数据进行类型转换    String->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(new WaterSensorMapFunction());
        //TODO 4.按照传感器id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 5.每种传感器输出最高的3个水位值
        SingleOutputStreamOperator<String> processDS = keyedDS.process(
                new KeyedProcessFunction<String, WaterSensor, String>() {
                    //定义一个ListState用于存放当前传感器对应的水位值
                    ListState<Integer> vcListState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        ListStateDescriptor<Integer> listStateDescriptor
                                = new ListStateDescriptor<Integer>("vcListState", Integer.class);
                        vcListState = getRuntimeContext().getListState(listStateDescriptor);
                    }

                    //ws1,2,30
                    //ws1,3,50
                    //ws1,4,40
                    //ws1,5,80
                    //ws1,6,20

                    @Override
                    public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        //获取当前采集的水位值
                        Integer vc = ws.getVc();
                        //将采集到的水位值放到状态中
                        vcListState.add(vc);
                        //排序
                        List<Integer> vcList = new ArrayList();
                        for (Integer vc1 : vcListState.get()) {
                            vcList.add(vc1);
                        }
                        vcList.sort((o1, o2) -> o2 - o1);
                        if (vcList.size() > 3) {
                            vcList.remove(3);
                        }
                        //vcListState.update(vcList);

                        out.collect("传感器id为" + ws.getId() + ",最大的3个水位值=" + vcList.toString());

                    }
                }
        );

        processDS.printToErr();

        env.execute();

    }
}
